package com.maddox.libcommon.utils;

import android.app.Application;

import java.lang.reflect.Method;

/**
 * 获取全局上下文的两种方式
 * 1:通过继承Application,保存一份全局静态变量
 * 2:利用反射从主线程中方法中获取
 */
public class AppGlobals {
    private static Application sApplication = null;

    public static Application getApplication() {
        if (sApplication == null) {
            try {
                Method method = Class.forName("android.app.ActivityThread").getDeclaredMethod("currentApplication");
                sApplication = (Application) method.invoke(null,(Object[])null);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return sApplication;
    }
}
